//
//  FactoryTypeViewController.m
//  MagictoLane
//
//  Created by 锋 on 2017/12/14.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "FactoryTypeViewController.h"
#import "FactoryCategoryModel.h"
#import "FactoryProductModel.h"

typedef NS_ENUM(NSInteger, MESSAGETYPE) {
    CATEGORYTYPE = 1,
    PRODUCTTYPE = 2,
};

@interface FactoryTypeViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *factoryTypeTableView;
@property (nonatomic, strong) NSArray *categoryArr;
@property (nonatomic, strong) NSArray *productArr;
@property (nonatomic, assign) MESSAGETYPE messageType;
@property (nonatomic, copy) NSString *categoryStr;
@property (nonatomic, copy) NSString *categoryId;
@property (nonatomic, copy) NSString *productStr;
@end

@implementation FactoryTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _messageType = CATEGORYTYPE;
    [self setupUI];
    [self setupFactoryTypeTableView];
    [self requestCategory];
    //    [self requestProduct];
    // Do any additional setup after loading the view.
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"工厂类型";
}

- (void)setupFactoryTypeTableView {
    _factoryTypeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _factoryTypeTableView.delegate = self;
    _factoryTypeTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _factoryTypeTableView.tableHeaderView = view;
    _factoryTypeTableView.tableFooterView = view;
    _factoryTypeTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_factoryTypeTableView];
}

- (void)requestCategory {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestCategoryFindAllParmers:nil Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _categoryArr = [FactoryCategoryModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             [_factoryTypeTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

- (void)requestProduct {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProductFindAllParmers:@{@"category.id":_categoryId} Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _productArr = [FactoryProductModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             [_factoryTypeTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_messageType == CATEGORYTYPE) {
        return _categoryArr.count;
    } else {
        return _productArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (_messageType == CATEGORYTYPE) {
        FactoryCategoryModel *model = _categoryArr[indexPath.row];
        cell.textLabel.text = model.name;
    } else {
        FactoryProductModel *model = _productArr[indexPath.row];
        cell.textLabel.text = model.name;
    }
    cell.textLabel.textColor = UIColorHexValue(0x4D4D4D);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_messageType == CATEGORYTYPE) {
        FactoryCategoryModel *model = _categoryArr[indexPath.row];
        _categoryStr = model.name;
        _categoryId = model.ID;
        _messageType = PRODUCTTYPE;
        [self requestProduct];
        [_factoryTypeTableView reloadData];
    } else {
        FactoryProductModel *model = _productArr[indexPath.row];
        _productStr = model.name;
        self.factoryTypeName([NSString stringWithFormat:@"%@/%@", _categoryStr, _productStr], model.ID);
        [self.navigationController popViewControllerAnimated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
